//
//  CombinedAnimationView.swift
//  TanTan_Kotlin
//
//  Created by EDY on 5/26/25.
//

import SwiftUI

struct CombinedAnimationView : View {
    @State private var isExpanded = false
    @State private var isRotated = false
    @State private var isCircled = false
    @State private var message = "请点击"
    @State private var backgroundColor: Color = .purple
    @State private var isLongPressed = false
    @State private var offset: CGSize = .zero
    
    var body: some View {
        VStack {
            Circle()
                .fill(.green)
                .frame(width: 100,height: 100)
                .offset(offset)
                .gesture(DragGesture().onChanged { gesture in
                    offset = gesture.translation
                }
                    .onEnded{ _ in
                        withAnimation(.easeInOut(duration: 0.5)) {
                            offset = .zero
                        }
                    }
                )
            
            RoundedRectangle(cornerRadius: 25)
                .fill(isLongPressed ? .orange : .red)
                .frame(width: isExpanded ? 200 : 100, height: isExpanded ? 200 : 100)
                .rotationEffect(.degrees(isRotated ? 180 : 0))
                .animation(.easeInOut(duration: 1), value: isExpanded)
                .animation(.easeInOut(duration: 1), value: isRotated)
                .onLongPressGesture(minimumDuration: 1.0,pressing: { isPressing in
                    withAnimation {
                        isLongPressed = isPressing
                    }
                }, perform: {
                    print("长按手势结束")
                })
               
            Button("切换"){
                withAnimation {
                    isExpanded.toggle()
                    isRotated.toggle()
                }
            }
            Circle()
                .fill(.purple)
                .frame(width: isCircled ? 200 : 100, height: isCircled ? 200 : 100)
                .animation(.default, value: isExpanded)
                .onTapGesture {
                    var transaction = Transaction(animation: .easeIn(duration: 1))
                    transaction.disablesAnimations = true
                    withTransaction(transaction) {
                        isCircled.toggle()
                    }
                }
            Button("控制动画"){
                var transaction = Transaction(animation: .easeIn(duration: 1))
                transaction.disablesAnimations = true
                withTransaction(transaction) {
                    isCircled.toggle()
                }
            }
            
            Text(message)
                .padding()
                .background(backgroundColor)
                .foregroundColor(.white)
                .cornerRadius(10)
                .onTapGesture {
                    withAnimation(.easeInOut(duration: 1)) {
                        message = "点过了"
                        backgroundColor = .blue
                    }
                
            }
        }
        
    }
}

#Preview {
    CombinedAnimationView()
}
